<?php

class Stories extends La_Controller {
	function __construct() {
		parent::__construct();
		$this->load->model(array("User_model", "Emergency_model"));
		if (!$this->authorize->is_admin_logged_in()) {
			la_redirect("/admin/login", "请先登录");
		}
		$this->data['nav'] = 'story';
	}
	
	function index() {
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$id = intval($this->input->post('id'))-1000000;
			if ($id < 1) {
				redirect("/admin");
			}
			$this->Emergency_model->update_status(0, $id);
			$data['type'] = $this->input->post("type");
			$data['target_type'] = "sc";
			$data['target_id'] = $id;
			$data['content'] = $this->input->post("content");
			$data['created_by'] = $this->authorize->current_admin_user();
			$data['created'] = date("Y-m-d H:i:s");
			$this->load->model("Feedback_model");
			$fid = $this->Feedback_model->add($data);
			
			la_redirect("console/stories/closed", "保存成功");
		} else {
			$this->load->view('admin/story_delete', $this->data);
		}
	}	
	public function closed() {
		$this->load->model("Feedback_model");
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$ids = $this->input->post('ids');
			$act = $this->input->post("act");
			if ($act == 'restore') {
				for ($i = 0, $n = count($ids); $i < $n; $i++) {
					$this->Emergency_model->update_status($ids[$i], 1);
					$this->Feedback_model->delete_by_target_id($ids[$i], "sc");
				}
			} else if ($act == 'delete') {
				for ($i = 0, $n = count($ids); $i < $n; $i++) {
					$this->Emergency_model->delete($ids[$i]);
					$this->Feedback_model->delete_by_target_id($ids[$i], "sc");
				}
			}
			la_redirect("console/stories/closed", "保存成功");
		} else {
			$offset = intval($this->uri->segment(4));
			$stories_count = $this->Feedback_model->get_count_all("sc");
			$this->load->library('pager');
			$config['base_url'] = site_url("console/stories/closed/");
			$config['total_rows'] = $stories_count;
			$config['per_page'] = LA_PAGE_SIZE;
			$config['uri_segment'] = 4;
			$this->pager->initialize($config);
			
			$stories = $this->Feedback_model->find_all("sc", $offset, $config['per_page']);
//			if (!empty($stories)) {
//				foreach ($stories as &$story) {
//					$feedback = $this->Feedback_model->find_feedbacks_by_target_id($story['id'],'sc');
//					if (empty($feedback)) continue;
//					$story['feedback_type'] = $feeback_type_array[$feedback[0]['type']];
//					$story['feedback_time'] =$feedback[0]['created'];
//				}
//			}
//			unset($group);
			$this->data['stories'] = $stories;
			$this->data['pager'] =  $this->pager->create_links();
			$this->load->view('admin/deleted_stories', $this->data);
		}
	}
	
	public function story_scroller() {
	if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$action = $this->input->post("action");
			$this->load->model("Log_model");
			$current_user = $this->authorize->current_admin_user();
			if ($action == "set") {
				$id = intval($this->input->post('id'))-1000000;
				if ($id < 1) {
					redirect("/console/stories");
				}
				$this->Emergency_model->top($id, 365, 1);
				
				$log = array('type' => 'top', 'created_by' => $current_user, 'created' => date("Y-m-d H:i:s")
										,'params' => json_encode(array('story_id' => $id, 'days' => 365, 'beckend'=> 1)));
				$this->Log_model->add($log);
			} else {
				$ids = $this->input->post("ids");
				for($i = 0, $n = count($ids); $i < $n; $i++) {
					$this->Emergency_model->untop($ids[$i]);
					$log = array('type' => 'top', 'created_by' => $current_user, 'created' => date("Y-m-d H:i:s")
										,'params' => json_encode(array('story_id' => $ids[$i],  'untop'=> 1)));
					$this->Log_model->add($log);
				}
			}
		}
		
		$stories = $this->Emergency_model->find_admin_top();
		$this->data['stories'] = $stories;
		$this->load->view('admin/story_scroller', $this->data);
	}
}